package com.mak001.lite.multiSkill;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import javax.swing.SwingUtilities;

import extras.mak001.AnimWatcher;
import extras.mak001.BankCache;
import com.mak001.lite.multiSkill.GUI.GUILite;
import com.mak001.lite.multiSkill.subs.AlcherLite;
import com.mak001.lite.multiSkill.subs.BoneBuryLite;
import com.mak001.lite.multiSkill.subs.CookerLite;
import com.mak001.lite.multiSkill.subs.FireMakerLite;
import com.mak001.lite.multiSkill.subs.PowerChopperLite;
import com.mak001.lite.multiSkill.subs.PowerFisherLite;
import com.mak001.lite.multiSkill.subs.RuneCrafterLite;
import com.mak001.lite.multiSkill.subs.SuperHeaterLite;
import com.mak001.lite.multiSkill.subs.Miners.Miner;
import com.mak001.lite.multiSkill.subs.Miners.RuneEssMiner;
import com.mak001.lite.multiSkill.subs.Miners.StairMiner;
import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.event.listeners.MessageListener;
import com.rsbuddy.event.listeners.PaintListener;
import com.rsbuddy.script.ActiveScript;
import com.rsbuddy.script.Manifest;
import com.rsbuddy.script.methods.Game;
import com.rsbuddy.script.util.Random;
import com.rsbuddy.script.util.Timer;
import com.rsbuddy.script.wrappers.Area;
import extras.salvation.api.methods.ExtendedPaint;
import extras.salvation.api.methods.ExtendedSkillInformation;

@Manifest(name = "Mnogi Skiller Lite", authors = { "MAK001" }, version = 0.102, description = "can do anything you can, except minigames and dungeoneering")
public class MainLite extends ActiveScript implements PaintListener,
		MouseListener, MessageListener, MouseMotionListener {

	public static long startTime = 0;
	public static Timer move;
	private subScript curr;
	GUILite gui;

	private int next = 1;

	private static int min;
	private static int max;
	private int looped = 0;
	public static boolean start = false;

	public static ArrayList<subScript> subs = new ArrayList<subScript>();
	public static ArrayList<Area> banks = new ArrayList<Area>();

	// subScript initiation
	public static BoneBuryLite bb;
	public static AlcherLite al;
	public static SuperHeaterLite sh;
	public static FireMakerLite fm;
	public static CookerLite cl;
	public static PowerChopperLite pc ;
	public static RuneCrafterLite rcer;
	public static PowerFisherLite fisher;

	// miners
	public static RuneEssMiner rem;
	public static Miner miner;
	public static StairMiner sminer;

	public static BankCache bankCache = new BankCache();
	public static boolean shutDown;

	public boolean onStart() {

		addBanks();
		move = new Timer(0);
		showGUI();
		while (!start) {
			sleep(500);
		}
		setFirstEnd();
		setSub(subs.get(0));
		ExtendedPaint.start();
		getContainer().submit(new AnimWatcher());
		startTime = System.currentTimeMillis();
		for (subScript s : subs) {
			if (s.getTime() != null) {
				s.getTime().reset();
				if (!s.getTime().isPaused()) {
					s.getTime().pause();
				}
			}
		}
		return true;
	}

	private void showGUI() {

		try {
			SwingUtilities.invokeAndWait(new Runnable() {

				public void run() {

					gui = new GUILite();
					gui.setVisible(true);
				}
			});
		} catch (InterruptedException e) {
		} catch (InvocationTargetException e) {
		}
	}

	private void addBanks() {// TODO

		banks.add(Methods.GrandExchange);
		banks.add(Methods.Draynor);
		banks.add(Methods.AlKharid);
		banks.add(Methods.VarrokEast);
		banks.add(Methods.VarrokWest);
		banks.add(Methods.Edgeville);
		banks.add(Methods.FaladorEast);
		banks.add(Methods.FaladorWest);
		banks.add(Methods.DuelArena);
		banks.add(Methods.Daemonheim);
	}

	@Override
	public int loop() {

		ExtendedSkillInformation.updateSkill(-1);
		if (getSub() != null) {
			if (move.getRemaining() == 0 && getSub().canSwitch() == true) {
				if (subs.size() > 1) {
					if (!getSub().getTime().isPaused()) getSub().getTime()
							.pause();
					setSub(getNext());
					nextP();
					Methods.move();
					Methods.didCheck = false;
				} else {
					stop();
				}
			} else {
				if (Methods.hasRequired(getSub())) {
					if (getSub().getTime() != null) if (getSub().getTime()
							.isPaused()) getSub().getTime().resume();
					getSub().loop();
				} else {
					Methods.getItems(getSub());
				}
			}
		}
		return 0;
	}

	private void setFirstEnd() {

		if (subs.size() > 1 || shutDown == true) {
			move.setEndIn(setEnd());
		} else {
			move.setEndIn(1000000000);
		}
	}

	public subScript getSub() {

		return curr;
	}

	public void setSub(subScript s) {

		curr = s;
	}

	@Override
	public void onRepaint(Graphics g2) {

		Graphics2D g = (Graphics2D) g2;
		ExtendedPaint.enableAntiAliasing(g);
		// TODO Auto-generated method stub
		ExtendedPaint.drawPaintButton(g);
		if (getSub() != null) {
			ExtendedPaint.drawMouse(g, ExtendedPaint.MOUSE_STYLE_CROSSHAIR,
					getSub().skill(), ExtendedPaint.FONT_STYLE_SHADOW,
					"Status:  " + getSub().getStatus());
			ExtendedPaint
					.drawInventory(g, getSub().skill(), getSub().getTime());
			ExtendedPaint.drawProgressBar(g,
					ExtendedPaint.POSITION_ABOVE_CHATBOX, getSub().skill());
			if (getSub().getPaintString() != null) ExtendedPaint
					.drawExtraInventory(g, getSub().skill(), getSub()
							.getPaintString());
			else ExtendedPaint.drawExtraInventory(g, getSub().skill(),
					new String[] { "" });
		} else {
			ExtendedPaint.drawMouse(g, ExtendedPaint.MOUSE_STYLE_CROSSHAIR, 26,
					ExtendedPaint.FONT_STYLE_SHADOW, "Status: Starting up");
		}
		// g.drawString("" + bankCache.getAmount(), 20, 90);
		if (start) {
			g.drawString("move in: " + Timer.format(move.getRemaining()), 20,
					90);
			g.drawString("sub NO: " + getCurrSubNO(), 20, 110);
			g.drawString("isIdle() = " + AnimWatcher.isIdle(), 20, 70);

			final Color color1 = new Color(255, 255, 255);
			final Color color2 = new Color(0, 0, 0);
			final Font font1 = new Font("Arial", 0, 9);
			g.setColor(color1);
			g.fillRect(445, 320, 72, 17);

			g.setColor(color2);
			g.drawRect(445, 320, 72, 17);
			g.setFont(font1);
			g.drawString("Next Sub", 458, 331);

			g.setColor(color1);
			g.fillRect(373, 320, 72, 17);
			g.setColor(color2);
			g.drawRect(373, 320, 72, 17);
			g.setFont(font1);
			g.drawString("Skip Check", 386, 331);

			if (Methods.didCheck == false) {
				g.drawString("didCheck = false", 20, 130);
			} else {
				g.drawString("didCheck = true", 20, 130);
			}

		}
	}

	private int getCurrSubNO() {

		int c = 0;
		for (int s = 0; s < subs.size(); s++) {
			if (subs.get(s).equals(getSub())) {
				c = s;
			}
		}
		return c;
	}

	public subScript getNext() {

		subScript nextS = null;
		for (int i = 0; i < subs.size() + 1; i++) {
			if (i == next) {
				try {
					nextS = subs.get(i);
				} catch (IndexOutOfBoundsException e) {
					if (shutDown == false) {
						looped++;
						java.util.Collections.shuffle(subs);
						nextS = subs.get(0);
					} else {
						Game.logout(false);
						stop();
					}
				}
			}
		}
		return nextS;
	}

	public void nextP() {

		if (next < subs.size()) {
			setNext(next + 1);
		} else {
			setNext(0);
		}
	}

	public void setNext(int i) {

		next = i;
	}

	public static int setEnd() {

		return Random.nextInt(min, max);
	}

	public static void setMinMax(String m1, String m2) {

		try {
			MainLite.min = Integer.parseInt(m1) * 60000;
			MainLite.max = Integer.parseInt(m2) * 60000;
		} catch (NumberFormatException e) {
			MainLite.min = 5 * 60000;
			MainLite.max = 30 * 60000;
		}
	}

	public void onFinish() {

		getSub().stop();
	}

	@Override
	public void mouseClicked(MouseEvent p) {

		ExtendedPaint.mouseClicked(p);
		Point m = p.getPoint();
		Rectangle r = new Rectangle(445, 320, 72, 17);
		Rectangle r1 = new Rectangle(373, 320, 72, 17);
		if (r.contains(m)) {
			move.setEndIn(0);
		} else if (r1.contains(m)) {
			Methods.didCheck = true;
		}
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {

	}

	@Override
	public void mouseExited(MouseEvent arg0) {

	}

	@Override
	public void mousePressed(MouseEvent arg0) {

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {

	}

	@Override
	public void messageReceived(MessageEvent e) {

		if (getSub() != null) {
			getSub().messageRecieved(e);
		}

	}

	@Override
	public void mouseDragged(MouseEvent arg0) {

	}

	@Override
	public void mouseMoved(MouseEvent m) {

		ExtendedPaint.mouseMoved(m);
	}

}
